package com.brick.controller;

import com.brick.entity.Account;
import com.brick.entity.AccountLog;
import com.brick.entity.Result;
import com.brick.entity.User;
import com.brick.service.AccountLogService;
import com.brick.service.AccountService;
import com.brick.service.UserService;
import com.brick.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import static com.brick.util.Util.util;


@RequestMapping("/api/userServlet")
@RestController
public class UserContraller {

    @Autowired
    UserService userService;
    @Autowired
    AccountService accountService;
    @Autowired
    AccountLogService accountLogService;

    @RequestMapping("/getUser")
    public Result<List<User>> getUser(@RequestBody User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultUtil.error(0,bindingResult.getFieldError().getDefaultMessage());
        }
        return ResultUtil.success(userService.selectAll(user));
    }

    @RequestMapping("/getAccount")
    public Result<Account> getAccount(@RequestBody Account account, BindingResult bindingResult){
        if(account.getUserId()==null){
            return ResultUtil.error(0,"查询帐户不能为空！");
        }
        if (bindingResult.hasErrors()){
            return ResultUtil.error(0,bindingResult.getFieldError().getDefaultMessage());
        }
        return ResultUtil.success(accountService.selectAll(account.getUserId()));
    }

    @RequestMapping("/addAccount")
    public Result<Account> addAccount(@RequestBody Account account, BindingResult bindingResult){
        if(account.getUserId()==null){
            return ResultUtil.error(0,"查询帐户不能为空！");
        }
        try{
            // 充值
            account.setUpdateBalance(account.getUpdateBalance());
            AccountLog accountLog = new AccountLog();
            accountLog.setId(util.getUUID());
            accountLog.setBalance(account.getUpdateBalance());
            accountLog.setDateline(util.getDateNow());
            accountLog.setUserId(account.getUserId());
            accountLog.setState(1);
            accountLogService.insert(accountLog);
            return ResultUtil.success(accountService.update(account));
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(0,bindingResult.getFieldError().getDefaultMessage());
        }
    }

    @Transactional
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user, BindingResult bindingResult){
        user.setId(util.getUUID());
        user.setDateline(util.getDateNow());
        if (user.getUserName() != null){
            try{
                // 开通帐户
                Account account = new Account();
                account.setId(util.getUUID());
                account.setBalance(0.0);
                account.setState(1);
                account.setUserId(user.getId());
                accountService.insert(account);
                return ResultUtil.success(userService.insert(user));
            }catch (Exception e){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResultUtil.error(0,bindingResult.getFieldError().getDefaultMessage());
            }

        }
        return ResultUtil.error(0,"用户名不能为空");
    }

    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user, BindingResult bindingResult){
        int i = 1;
        if (bindingResult.hasErrors()){
            return ResultUtil.error(0,bindingResult.getFieldError().getDefaultMessage());
        }
        return ResultUtil.success(userService.update(user));
    }
}
